#!/bin/bash
CI_REGISTRY=harbor.aixcoder.com
CI_REGISTRY_USER=$1
CI_REGISTRY_PASSWORD=$2
CI_REGISTRY_IMAGE=$3
VERSION=$4
if [ ! ${CI_REGISTRY_USER} ]; then
  echo "请输入用户名"
  exit 1
fi
if [ ! ${CI_REGISTRY_PASSWORD} ]; then
  echo "请输入密码"
  exit 1
fi
if [ ! ${CI_REGISTRY_IMAGE} ]; then
  echo "请输入镜像名称"
  exit 1
fi

docker login --username=$CI_REGISTRY_USER "$CI_REGISTRY" --password=$CI_REGISTRY_PASSWORD

# 创建并使用一个新的 multi-platform capable builder
docker buildx create --name formula2codeBuilder
docker buildx use formula2codeBuilder

# 构建并推送多架构镜像
docker buildx build --progress=plain --platform linux/amd64 -t $CI_REGISTRY/$CI_REGISTRY_IMAGE/formula2code:$VERSION -f Dockerfile . --push

# 将上一个命令的退出状态保存到变量中
result=$?

# 检查上一个命令的执行结果
if [ $result -eq 0 ]; then
  echo "命令执行成功"
else
  echo "命令执行失败，退出状态为 $result"
fi

# 删除本地的镜像
docker buildx rm formula2codeBuilder

if [ $result -ne 0 ]; then
  echo "fail to push image to harbor"
  exit 1
fi